<%@ page contentType="text/html; charset=UTF-8" %>
<%@ include file="/common/common.jsp" %>
<%@ page import="com.cinsea.common.Page" %>
<%@ page import="com.cinsea.query.model.Querydetail" %>
<%@ page import="com.cinsea.query.service.QueryService" %>
<%@ page import="com.cinsea.workflow.service.WfprocesslogService" %>
<%@ page import="com.cinsea.hrm.model.Human" %>
<%@ page import="com.cinsea.workflow.service.WfnodeService" %>
<%@ page import="com.cinsea.workflow.service.WfdefineService" %>
<%@ page import="com.cinsea.workflow.model.*" %>
<%@ page import="com.alibaba.fastjson.JSON" %>
<%
    response.setHeader("cache-control", "no-cache");
    response.setHeader("pragma", "no-cache");
    response.setHeader("expires", "Mon 1 Jan 1990 00:00:00 GMT");
    Option option = null;
    WfprocesslogService wfprocesslogService = (WfprocesslogService) Env.getBean("wfprocesslogService");
    WfnodeService wfnodeService = (WfnodeService) Env.getBean("wfnodeService");
    WfdefineService wfdefineService = (WfdefineService) Env.getBean("wfdefineService");
    String action = request.getParameter("action");
    String waitoperate = StringUtil.null2String(request.getParameter("waitoperate"));
    String userid = loginUser.getId();
    HumanService humanService = (HumanService) Env.getBean("humanService");
    String tablename = "cs_wfprocess";
    QueryService queryService = (QueryService) Env.getBean("queryService");
    Page pageObject = (Page) request.getAttribute("pageObject");
    String clear = "";
    clear = StringUtil.null2String(request.getParameter("clear"));
    String objno = "";
    String flowno = "";
    String title = "";
    String dowid = "";
    String finished = "";
    String creator = "";
    String createdatefrom = "";
    String createdateto = "";
    String readerStr = "{name:'processid'},{name:'objno'},{name:'id'},{name:'workflowid'},{name:'emergency'},{name:'creator'},{name:'createtime'},{name:'finished'},{name:'nodeid'},{name:'showworkitem'},{name:'workitems'}";
    String colStr = "";
    colStr += "{header:'" + I18n.getText("number") + "',width: 100,sortable: true,dataIndex:'objno'}";
    colStr += ",{header:'" + I18n.getText("name") + "',width: 370,sortable: true,dataIndex:'id'}";
    colStr += ",{header:'" + I18n.getText("workflow") + "',width: 120,sortable: true,dataIndex:'workflowid'}";
    colStr += ",{header:'" + I18n.getText("emergency") + "',width: 70,sortable: true,dataIndex:'emergency'}";
    colStr += ",{header:'" + I18n.getText("creator") + "',width: 70,sortable: true,dataIndex:'creator'}";
    colStr += ",{header:'" + I18n.getText("createtime") + "',width: 140,sortable: true,dataIndex:'createtime'}";
    colStr += ",{header:'" + I18n.getText("finished") + "',width: 60,sortable: true,dataIndex:'finished'}";
    colStr += ",{header:'" + I18n.getText("node") + I18n.getText("name") + "',width: 150,sortable: true,dataIndex:'nodeid'}";
    colStr += ",{header:'',width: 100,sortable: true,dataIndex:'showworkitem'}";
    //colStr += ",{header:'',sortable: true,dataIndex:'workitems'}";
    List jsondata = new ArrayList();
    if (pageObject.getTotalSize() != 0) {
        Wfprocess wfprocess = null;
        List list = (List) pageObject.getResult();
        for (int i = 0; i < list.size(); i++) {
            Map map = (Map) list.get(i);
            String fieldvalue = "";
            List jsonArray = new ArrayList();
            jsonArray.add(StringUtil.null2String(map.get("id")));
            List nodelist = wfprocesslogService.getCurrentNodeIds(map.get("id") == null ? null : map.get("id").toString());
            Wfnode wfnode = new Wfnode();
            if (nodelist.size() > 0)
                wfnode = wfnodeService.get((String) nodelist.get(0));
            jsonArray.add(StringUtil.null2String(map.get("objno")));
            String received=StringUtil.null2String(map.get("received"));
            String iconnew = iconnew = "";
            if (received.equals("-1"))
                iconnew = "<img src='" + request.getContextPath() + "/images/base/new1.gif'>";
            if (received.equals("0")) {
                iconnew = "<img src='" + request.getContextPath() + "/images/base/new.gif'>";
                ;
            }

            fieldvalue = iconnew+"<a href=\"javascript:onUrl('/workflow/wfprocess.jsp?processid=" + map.get("id") + "','" + StringUtil.filterJString2(StringUtil.null2String(map.get("title"))) + "','tab" + map.get("id") + "')\" >" + StringUtil.null2String(map.get("title")) + "</a>";
            jsonArray.add(fieldvalue);
            String workflowinfoname = wfdefineService.getWorkflowNames(StringUtil.null2String(map.get("workflowid")));
            fieldvalue = StringUtil.null2String(workflowinfoname);
            jsonArray.add(fieldvalue);
            option = optionService.get(map.get("emergency") == null ? null : map.get("emergency").toString());
            fieldvalue = StringUtil.null2String(option.getObjname());
            jsonArray.add(fieldvalue);
            fieldvalue = "<a href=\"javascript:onUrl('/form/formprocess.jsp?processid=" + humanService.getHumanById(map.get("creator") == null ? null : map.get("creator").toString()).getId() + "','" + humanService.getHumanById(map.get("creator") == null ? null : map.get("creator").toString()).getObjname() + "','tab" + humanService.getHumanById(map.get("creator") == null ? null : map.get("creator").toString()).getId() + "')\" >" + humanService.getHumanById(map.get("creator") == null ? null : map.get("creator").toString()).getObjname() + "</a>";
            jsonArray.add(fieldvalue);
            String createdatatime = map.get("createdate") + "&nbsp;" + map.get("createtime");
            fieldvalue = StringUtil.null2String(createdatatime);
            jsonArray.add(fieldvalue);
            String isfinish = "是";
            if ((map.get("finished") == null ? "" : map.get("finished").toString()).toString().equals("0")) {
                isfinish = "否";
            }
            fieldvalue = StringUtil.null2String(isfinish);
            jsonArray.add(fieldvalue);
            fieldvalue = StringUtil.null2String(wfnode.getObjname());
            jsonArray.add(fieldvalue);
            fieldvalue = "<a href=\"javascript:showoperator('" + request.getContextPath() + "/workflow/wfworkitem.jsp?processid=" + map.get("id") + "&nodeid=" + wfnode.getId() + "')\" id=\"" + map.get("id") + "\">操作者列表</a>";
            jsonArray.add(fieldvalue);
            /*List userlist = wfprocesslogService.getCurrentNodeUsers(map.get("id") == null ? null : map.get("id").toString());
            Human human = new Human();
            if (userlist.size() > 0)
                human = humanService.getHumanById((String) userlist.get(0));
            fieldvalue = StringUtil.null2String(human.getObjname());
            jsonArray.add(fieldvalue);*/
            jsonArray.add(fieldvalue);
            jsondata.add(jsonArray);
        }
    }

    pagemenustr += "addBtn(tb,'" + I18n.getText("quicksearch") + "','S','zoom',function(){onSearch2()});";
    pagemenustr += "addBtn(tb,'" + I18n.getText("clear") + "','R','erase',function(){reset()});";
    pagemenustr += "addBtn(tb,'" + I18n.getText("advancedsearch") + "','A','zoom_in',function(){onSearch3()});";
    pagemenustr += "addBtn(tb,'批量提交','A','zoom_in',function(){operationAll()});";

%>
<html>
<head>
    <%@ include file="/common/head.jsp" %>
    <script type="text/javascript">
        var showOperatorsDiloge;
        var selected = new Array();

        Ext.onReady(function() {

            Ext.QuickTips.init();
            <%if(!pagemenustr.equals("")){%>
            var tb = new Ext.Toolbar();
            tb.render('pagemenubar');
            <%=pagemenustr%>
            <%}%>
            var xg = Ext.grid;

            // shared reader
            var reader = new Ext.data.ArrayReader({}, [
                <%=readerStr%>
            ]);

            var sm = new Ext.grid.CheckboxSelectionModel();

            store = new Ext.data.GroupingStore({
                reader: reader,
                data: xg.dummyData,
                sortInfo:{field: 'createtime', direction: "DESC"},
                groupField:'workflowid'
            });

            var grid = new xg.GridPanel({
                region: 'center',
                store: store,
                columns: [sm,
                    <%=colStr%>
                ],
                sm:sm ,
                view: new Ext.grid.GroupingView({
                    forceFit:true,
                    startCollapsed:true,
                    sortAscText:'<%=I18n.getText("sortAscText")%>',
                    sortDescText:'<%=I18n.getText("sortDescText")%>',
                    groupByText:'<%=I18n.getText("groupByText")%>',
                    showGroupsText:'<%=I18n.getText("showGroupsText")%>',
                    columnsText:'<%=I18n.getText("columnsText")%>',
                    groupTextTpl: '{text} ({[values.rs.length]})'
                }),
                frame:true,
                collapsible: false,
                animCollapse: true,
                //title: '列表',
                iconCls: 'icon-grid'
            });
            //Viewport
            //ie6 bug                                                                     +

            store.on('load', function(st, recs) {
                for (var i = 0; i < recs.length; i++) {
                    var reqid = recs[i].get('processid');
                    for (var j = 0; j < selected.length; j++) {
                        if (reqid == selected[j]) {
                            sm.selectRecords([recs[i]], true);
                        }
                    }
                }
            }
                    );

            sm.on('rowselect', function(selMdl, rowIndex, rec) {
                var reqid = rec.get('processid');
                for (var i = 0; i < selected.length; i++) {
                    if (reqid == selected[i]) {
                        return;
                    }
                }
                selected.push(reqid)
            }
                    );
            sm.on('rowdeselect', function(selMdl, rowIndex, rec) {
                var reqid = rec.get('processid');
                for (var i = 0; i < selected.length; i++) {
                    if (reqid == selected[i]) {
                        selected.remove(reqid)
                        return;
                    }
                }

            }
                    );
            Ext.get('divSearch').setVisible(true);
            var viewport = new Ext.Viewport({
                layout: 'border',
                items: [
                    {region:'north',autoScroll:true,contentEl:'divSearch',split:true,collapseMode:'mini'},
                    grid
                ]
            });
            showOperatorsDiloge = new Ext.Window({
                layout:'border',
                closeAction:'hide',
                plain: true,
                modal :true,
                width:viewport.getSize().width * 0.8,
                height:viewport.getSize().height * 0.8,
                buttons: [
                    {
                        text     : '<%=I18n.getText("close")%>',
                        handler  : function() {
                            showOperatorsDiloge.hide();
                            showOperatorsDiloge.getComponent('dlgpanel').setSrc('about:blank');
                        }

                    }
                ],
                items:[
                    {
                        id:'dlgpanel',
                        region:'center',
                        xtype     :'iframepanel',
                        frameConfig: {
                            autoCreate:{id:'dlgframe', name:'dlgframe', frameborder:0} ,
                            eventsFollowFrameLinks : false
                        },
                        autoScroll:true
                    }
                ]
            });
            showOperatorsDiloge.render(Ext.getBody());
        });
        // Array data for the grids
        Ext.grid.dummyData = <%=JSON.toJSONString(jsondata)%>;
    </script>
</head>
<body>


<div id="divSearch" style="display:none;">
    <div id="pagemenubar">
    </div>
    <!--页面菜单结束-->
    <form action="${ctx}/wfprocess.do?action=<%=action%>&from=list" id="mainForm" name="mainForm" method="post">

        <table id="myTableObj" class="formtable">
            <tr>
                <td class="formlabel" width=10%><%=I18n.getText("workflow")%>NO</td>
                <td class="formvalue" width=23%><input name="objno" value="<%=objno%>" class="InputStyle2"
                                                               style="width:95%"></td>
                <td class="formlabel" width=10%><%=I18n.getText("workflow")%><%=I18n.getText("name")%>
                </td>
                <td class="formvalue" width=23%><input name="title" value="<%=title%>" class="InputStyle2"
                                                        style="width:95%"></td>
                <td class="formlabel" width=10%><%=I18n.getText("workflow")%><%=I18n.getText("type")%>
                </td>
                <!--流程类型 -->
                <%
                    String workflowname = "";
                    if (!StringUtil.isEmpty(dowid)) {
                        workflowname = wfdefineService.getWorkflowNames(dowid);// StringUtil.null2String(wfdefineService.getFree(dowid).getObjname());
                    }
                %>
                <td class="formvalue" width=23%>
                    <input type="hidden" name="dowid" value="<%=dowid%>"/>
                    <button class=Browser type="button"
                            onclick="javascript:getBrowser('${ctx}/browser/browser.jsp?id=17','dowid','dowidspan','0');"></button>
                    <span id="dowidspan"><%=workflowname%></span>
                </td>
            </tr>
            <tr>
                <%
                    Human human = null;
                    String humanname = "";
                    if (!StringUtil.isEmpty(creator)) {
                        human = humanService.getHumanById(creator);
                    } else {
                        creator = StringUtil.null2String(request.getAttribute("creator"));
                        if (!StringUtil.isEmpty(creator)) {
                            human = humanService.getHumanById(creator);
                        }
                    }
                    if (human != null) {
                        creator = human.getId();
                        humanname = StringUtil.null2String(human.getObjname());
                    }
                %>
                <td class="formlabel"><%=I18n.getText("creator")%>
                </td>
                <td class="formvalue">
                    <button class=Browser type="button"
                            onclick="javascript:getBrowser('${ctx}/browser/browser.jsp?id=4','creator','creatorspan','0');"></button>
                    <span id=creatorspan><%=humanname%></span>
                    <input type=hidden id="creator" name=creator value="<%=creator%>">
                </td>
                <td class="formlabel"><%=I18n.getText("createdate")%>
                </td>
                <td class="formvalue">
                    <input type=text class=inputstyle size=10 name="createdatefrom" value="<%=createdatefrom%>"
                           onclick="WdatePicker()">-
                    <input type=text class=inputstyle size=10 name="createdateto" value="<%=createdateto%>"
                           onclick="WdatePicker()">
                </td>
                <td class="formlabel"><%=I18n.getText("status")%>
                </td>
                <td class="formvalue">
                    <select name=finished disabled="true"><!-- finished-->
                        <option value="-1" <%if (finished.equals("-1")) {%> selected <%}%> ></option>
                        <option value="1" <%if (finished.equals("1")) {%>
                                selected <%}%> ><%=I18n.getText("end")%>
                        </option>
                        <option value="0" <%if (finished.equals("0")||waitoperate.equals("1")) {%> selected <%}%> ><%=I18n.getText("inprocess")%>
                        </option>
                    </select>
                </td>
            </tr>

        </table>
        <div id="divObj" style="display:none">
            <table id="displayTable">
                <thead>
                <tr>
                    <th colspan="8" style="background-color:#f7f7f7;height:20"><b><a href="javascript:void(0)"
                                                                                     style="color:green"><%=I18n.getText("currentnodeactorlist")%>
                        :</a></b></th>
                </tr>
                <tr style="background-color:#f7f7f7;height:20">
                    <!--<th align="center"><b>ObjId</b></th>-->
                    <th align="center"><b><%=I18n.getText("type")%>
                    </b></th>
                    <th align="center"><b><%=I18n.getText("actor")%>
                    </b></th>
                </tr>
                </thead>
                <tbody id="mytbody" name="mytbody"><!-- 在这刷新 -->
                </tbody>
            </table>
        </div>
    </form>
</div>
<script language="javascript" type="text/javascript">


    function showoperator(url) {
        this.showOperatorsDiloge.getComponent('dlgpanel').setSrc(url);

        this.showOperatorsDiloge.show()

    }


    var opttype = function(data) {
        return data.type
    };

    var operator = function(data) {
        return '<a href="javascript:onUrl(\"/form/formprocess.jsp?processid=' + data.objid + '\",\"' + data.name + '\",\"tab' + data.objid + '\")">' + data.name + '</a>'
    };

    function onSearch(pageno) {
        document.mainForm.pageno.value = pageno;
        document.mainForm.submit();
    }

    function onSearch2() {
        document.getElementById('mainForm').action = "${ctx}/wfprocess.do?action=<%=action%>&finished=0";
        document.getElementById('mainForm').submit();

    }

    function onSearch3() {

        document.getElementById('mainForm').action = "${ctx}/workflow/wfprocesssearch.jsp";
        document.getElementById('mainForm').submit();
    }
    function reset() {
        document.forms[0].objno.value = "";
        document.forms[0].title.value = "";
        document.forms[0].dowid.value = "";
        document.getElementById("dowidspan").innerText = "";
        document.getElementById("creator").value = "";
        document.getElementById("creatorspan").innerText = "";
        document.forms[0].createdatefrom.value = "";
        document.forms[0].createdateto.value = "";
    }

    function getBrowser(viewurl, inputname, inputspan, isneed) {
        var id;
        try {
            id = showDialog(viewurl);
        } catch(e) {
        }
        if (id != null) {
            if (id[0] != '0') {
                document.getElementById(inputname).value = id[0];
                document.getElementById(inputspan).innerHTML = id[1];
            } else {
                document.getElementById(inputname).value = '';
                if (isneed == '0')
                    document.getElementById(inputspan).innerHTML = '';
                else
                    document.getElementById(inputspan).innerHTML = '<img src=${ctx}/images/base/checkinput.gif>';

            }
        }
    }


    function operationAll() {
        if (selected.length == 0) {
            Ext.Msg.buttonText = {ok:'<%=I18n.getText("ok")%>'};
            Ext.MessageBox.alert('', '<%=I18n.getText("selectConfirm")%>');
            return;
        }
        Ext.Msg.buttonText = {yes:'<%=I18n.getText("yes")%>',no:'<%=I18n.getText("no")%>'};
        Ext.MessageBox.confirm('', '<%=I18n.getText("processconfirm")%>', function (btn, text) {
            if (btn == 'yes') {
                Ext.Ajax.request({
                    url: '${ctx}/workflow.do?action=batchoperate',
                    params:{ids:selected.toString()},
                    success: function(returnstr) {
                        document.location.reload();
                    }
                });
            } else {
                //                         selected=[];
                //                        store.load({params:{start:0, limit:20}});
            }
        });

    }
    //*********************************得到网页中元素的绝对位置(start)*********************************//
    function GetAbsoluteLocation(element) {
        if (arguments.length != 1 || element == null) {
            return null;
        }
        var offsetTop = element.offsetTop;
        var offsetLeft = element.offsetLeft;
        var offsetWidth = element.offsetWidth;
        var offsetHeight = element.offsetHeight;
        while (element = element.offsetParent) {
            offsetTop += element.offsetTop;
            offsetLeft += element.offsetLeft;
        }
        return { absoluteTop: offsetTop, absoluteLeft: offsetLeft,
            offsetWidth: offsetWidth, offsetHeight: offsetHeight };
    }
    //*********************************得到网页中元素的绝对位置(end)*********************************//
</script>
<script type="text/javascript" language="javascript" src="${ctx}/datapicker/WdatePicker.js"></script>
</body>
</html>
